#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include <fdb.h>
#include <fdb-helpers.h>

int update(query_t *query, void *ctx) {
	char *data, *newdata;
	size_t size, newsize;

	query_data_get(query, 1, &data, &size);

	newsize = size + 1;
	newdata = malloc(newsize);
	memcpy(&newdata[1], data, size);
	newdata[0] = 'x';

	query_data_set(query, NULL, 0, newdata, newsize);

	free(newdata);

	return 0;
}

int main(int argc, char *argv[]) {
	char *data;
	size_t size;
	database_t *database;
	transaction_t *transaction;
	query_t *query;
	result_e result;

	if (argc < 2) {
		fprintf(stderr, "Usage: %s <database>\n", argv[0]);
		return 1;
	}

	database = database_open(argv[1]);
	if (database == NULL)
		return 1;

	transaction = transaction_begin(database, READ_COMMITTED);
	query = query_new(transaction);

	query_prepare(query, QUERY_UPDATE, "data");
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "Aaron\n", 7),
	                               NULL);
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "Usenet\n", 7),
	                               NULL);
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "deploy\n", 7),
	                               NULL);
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "outbroken\n", 11),
	                               NULL);
	query_append_conditions(query, condition_predicate_new(1, "eq", 1, "zygotes\n", 9),
	                               NULL);
	query_update_callback(query, update, NULL);
	query_execute(query);
	query_finalize(query);

	query_destroy(query);

	transaction_commit(transaction);
	database_close(database);

	return 0;
}


